package com.app.xinfadi.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;

import com.app.xinfadi.R;


public class SlidButton extends View implements OnTouchListener {
	/**
	 * true 打开 推送关闭
	 * 
	 */
	private boolean nowChoose = false;// 记录当前按钮是否打开，true为打开，false为关闭

	private boolean onSlip = false;// 记录用户是否在滑动
	private float downX, nowX; // 按下时的x，当前的x

	private boolean isChgLsnOn = false;// 是否设置监听
	private OnChangedListener changedLis;

	int begin, end;// 控件当前的位置

	private Bitmap bg_on, bg_off, slip_btn;
	private String TAG = SlidButton.class.getSimpleName();
	private int bgWidth = 0;
	private int bgHeight = 0;

	public SlidButton(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public SlidButton(Context context) {
		super(context);
		init();
	}

	private void init() {
		// 载入图片资源
		bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.bg_game_switch_open);
		bg_off = BitmapFactory.decodeResource(getResources(), R.drawable.bg_game_switch_close);
		slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.btn_game_handle);
		bgWidth = bg_off.getWidth();
		bgHeight = bg_off.getHeight();
		setOnTouchListener(this);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		if (bg_off != null) {
			float x = 0;
			// if (nowX<(bg_on.getWidth()/2)) //滑动到前半段与后半段的背景不同,在此做判断
			if (!nowChoose)
				canvas.drawBitmap(bg_off, 0, 0, null);// 画出关闭时的背景
			else
				canvas.drawBitmap(bg_on, 0, 0, null);// 画出打开时的背景

			if (onSlip) {// 是否是在滑动状态,
				if (nowX >= bg_on.getWidth() - slip_btn.getWidth())// 是否划出指定范围,不能让游标跑到外头,必须做这个判断
					x = bg_on.getWidth() - slip_btn.getWidth();// 减去游标1/2的长度
				else if (nowX <= 0)
					x = 0;
				else {
					x = nowX;
				}
			} else {
				if (nowChoose)// 根据现在的开关状态设置画游标的位置
					// 打开
					x = bg_on.getWidth() - slip_btn.getWidth();
				else
					x = 0;
			}

			// if (x < 0) // 对游标位置进行异常判断..
			// x = 0;
			// else if (x > bg_on.getWidth() - slip_btn.getWidth())
			// x = 0;

			canvas.drawBitmap(slip_btn, x + begin, 0, null);// 画出游标.
		}
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {

		switch (event.getAction()) {// 根据动作来执行代码

		case MotionEvent.ACTION_MOVE:// 滑动
			nowX = event.getX();
			break;
		case MotionEvent.ACTION_DOWN:// 按下
			if (event.getX() < begin)
				return false;
			onSlip = true;
			downX = event.getX();
			nowX = downX;
			break;
		case MotionEvent.ACTION_UP:// 松开
			onSlip = false;
			boolean lastChoose = nowChoose;
			/*
			 * if (event.getX() >= begin+(bg_on.getWidth()/2)) nowChoose = true;
			 * else nowChoose = false;
			 */
			nowChoose = !nowChoose;
			if (isChgLsnOn && (lastChoose != nowChoose))// 如果设置了监听器,就调用其方法.
				changedLis.OnChanged(nowChoose);
			break;
		default:
			break;
		}
		invalidate();
		return true;
	}

	/**
	 * EXACTLY：一般是设置了明确的值或者是MATCH_PARENT
	 * 
	 * AT_MOST：表示子布局限制在一个最大值内，一般为WARP_CONTENT
	 * 
	 * UNSPECIFIED：表示子布局想要多大就多大，很少使用
	 */

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		// if (bg_on != null) {
		// LogUtil.e(TAG, "if (bg_on != null) {");
		// setMeasuredDimension(bg_on.getWidth(), bg_on.getHeight() +
		// getPaddingTop() + getPaddingBottom());
		// }

		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);
		int measureWidth;
		int measureHeight;

		if (widthMode == MeasureSpec.EXACTLY) {
			measureWidth = widthSize;
		} else {
			int desired = (int) (getPaddingLeft() + bgWidth + getPaddingRight());
			measureWidth = desired;
		}

		if (heightMode == MeasureSpec.EXACTLY) {
			measureHeight = heightSize;
		} else {
			int desired = (int) (getPaddingTop() + bgHeight + getPaddingBottom());
			measureHeight = desired;
		}
		setMeasuredDimension(measureWidth, measureHeight);
	}

	public void SetOnChangedListener(OnChangedListener l) {// 设置监听器,当状态修改的时候
		isChgLsnOn = true;
		changedLis = l;
	}

	public interface OnChangedListener {
		abstract void OnChanged(boolean checkState);
	}

	public boolean isNowChoose() {
		return nowChoose;
	}

	public void setNowChoose(boolean nowChoose) {
		this.nowChoose = nowChoose;
	}
}